# file = open("Foundations\mymodule.py", "r")
# content = file.read()
# print(f"读取的文件内容如下: \n{content}")

# file = open("Foundations\mymodule.py", "r")
# line = file.readline() # 只能读取一行，可以向其中添加参数，取字符串的索引
# print(line.strip())  # 使用 strip() 去除每行末尾的换行符
# file.close # 关闭文件，释放资源，想象手动操作时的情景

# file = open("Foundations\mymodule.py", "r")
# lines = file.readlines()  # 返回的是一个列表
# print(lines)
# for line in lines:
#     print(line.strip())  # 使用 strip() 去除每行末尾的换行符
# file.close # 关闭文件，释放资源，想象手动操作时的情景

# write、writelines 是对文件的覆盖写
with open("example.txt", "w") as file:
    file.write("with open; 1st write \n")

with open("example.txt", "w") as file:
    file.writelines("with open; 1st writelines \n")

with open("example.txt", "r") as file:
    content = file.read()
    print(content)

# 对文件的追加写应该这样
with open("example.txt", "a") as file:
    content = file.write("append line2")
with open("example.txt", "r") as file:
    content = file.read()
    print(content)